package cn.aws360.boot.system.starter.controller;

import cn.aws360.boot.base.api.annotation.OpsLog;
import cn.aws360.boot.base.api.constant.enums.OpsTypeEnum;
import cn.aws360.boot.base.api.pojo.response.ResultModel;
import cn.aws360.boot.base.api.utils.ServletUtils;
import cn.aws360.boot.base.api.validation.ValidationGroup;
import cn.aws360.boot.data.api.pojo.request.PageQuery;
import cn.aws360.boot.data.api.pojo.response.PageResult;
import cn.aws360.boot.system.api.pojo.request.SysDeptQuery;
import cn.aws360.boot.system.api.pojo.request.SysDeptWrite;
import cn.aws360.boot.system.api.pojo.response.SysDeptModel;
import cn.aws360.boot.system.biz.service.SysDeptService;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.lang.tree.Tree;
import com.alibaba.fastjson.JSONObject;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static cn.aws360.boot.system.api.constant.SysConstant.*;

/**
 * @author junnan
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/sys/dept")
@ApiSupport(order = MODULE_DEPT_CODE)
@Api(value = MODULE_DEPT_NAME, tags = MODULE_DEPT_TAGS)
public class SysDeptController {

    private final SysDeptService deptService;

    @GetMapping("/index.json")
    @SaCheckPermission("sys:dept:query")
    @ApiOperation(value = "部门页面", hidden = true)
    public ResultModel<JSONObject> index() {
        return ResultModel.ok(ServletUtils.getPageJson());
    }

    @GetMapping("/list")
    @SaCheckPermission("sys:dept:query")
    @ApiOperation(value = "部门列表")
    @ApiOperationSupport(order = 1)
    public ResultModel<List<SysDeptModel>> list(SysDeptQuery sysDeptQuery) {
        return ResultModel.ok(deptService.findList(sysDeptQuery));
    }

    @GetMapping("/page")
    @SaCheckPermission("sys:dept:query")
    @ApiOperation(value = "部门分页")
    @ApiOperationSupport(order = 1)
    public ResultModel<PageResult<SysDeptModel>> page(SysDeptQuery sysDeptQuery, PageQuery pageQuery) {
        return ResultModel.ok(deptService.findPage(sysDeptQuery, pageQuery));
    }

    @GetMapping("/tree")
    @SaCheckPermission("sys:dept:query")
    @ApiOperation(value = "部门树形")
    @ApiOperationSupport(order = 1)
    public ResultModel<List<Tree<Integer>>> tree(SysDeptQuery sysDeptQuery) {
        return ResultModel.ok(deptService.findTree(sysDeptQuery));
    }

    @GetMapping("/{id}")
    @SaCheckPermission("sys:dept:detail")
    @ApiOperation(value = "部门详情")
    @ApiOperationSupport(order = 1)
    @ApiImplicitParam(name = "id", value = "部门 id", dataTypeClass = Integer.class, required = true)
    public ResultModel<SysDeptModel> detail(@PathVariable Integer id) {
        return ResultModel.ok(deptService.detail(id));
    }

    @PostMapping
    @SaCheckPermission("sys:dept:add")
    @OpsLog(title = "新增部门", opsType = OpsTypeEnum.INSERT)
    @ApiOperation(value = "新增部门")
    @ApiOperationSupport(order = 2)
    public ResultModel<Boolean> add(@Validated(ValidationGroup.add.class) @RequestBody SysDeptWrite sysDeptWrite) {
        return ResultModel.ok(deptService.add(sysDeptWrite));
    }

    @PutMapping()
    @SaCheckPermission("sys:dept:edit")
    @OpsLog(title = "修改部门", opsType = OpsTypeEnum.UPDATE)
    @ApiOperation(value = "修改部门")
    @ApiOperationSupport(order = 3)
    public ResultModel<Boolean> edit(@Validated(ValidationGroup.edit.class) @RequestBody SysDeptWrite sysDeptWrite) {
        return ResultModel.ok(deptService.edit(sysDeptWrite));
    }

    @DeleteMapping("/{ids}")
    @SaCheckPermission("sys:dept:remove")
    @OpsLog(title = "移除部门", opsType = OpsTypeEnum.REMOVE)
    @ApiOperation(value = "移除部门")
    @ApiOperationSupport(order = 4)
    @ApiImplicitParam(name = "ids", value = "部门 ids", dataTypeClass = String.class, required = true)
    public ResultModel<Boolean> remove(@PathVariable String ids) {
        return ResultModel.ok(deptService.remove(ids));
    }

}
